package com.js.nickcar.module.addcar.brand;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.js.nickcar.R;
import com.js.nickcar.base.BaseHolder;
import com.js.nickcar.entity.remote.CarBrandBean;
import com.zyzd.commontools.log.TLog;
import com.zyzd.commontools.screen.TDevice;

import java.util.ArrayList;

/**
 * Created by LZY on 2017/9/29.
 */

public class HotCarBrandAdapter extends RecyclerView.Adapter<HotCarBrandAdapter.HotCarBrandItemHolder> {

    public final Context mContext;
    public final LayoutInflater mInflater;
    public ArrayList<CarBrandBean> mHotCarBrandList = new ArrayList<>();

    public HotCarBrandAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext.getApplicationContext());
    }

    @Override
    public HotCarBrandItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HotCarBrandItemHolder(mInflater.inflate(R.layout.item_hot_car_brand, parent, false));
    }

    @Override
    public void onBindViewHolder(HotCarBrandItemHolder holder, final int position) {
        final CarBrandBean item = getItem(position);
        TLog.d("zyzd", "HotCarBrandAdapter>>onBindViewHolder--> " + item.getImage());
        Glide.with(mContext)
                .load(item.getImage())
                .override(TDevice.dpToPx(30), TDevice.dpToPx(30))
                .into(holder.ivBrand);
        holder.tvBrandName.setText(item.getBrand_name());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onHotItemClickListner!=null){
                    onHotItemClickListner.onHotItemClick(item,position);
                }
            }
        });
    }

    public CarBrandBean getItem(int position) {
        return mHotCarBrandList.get(position);
    }

    @Override
    public int getItemCount() {
        return mHotCarBrandList.size() > 10 ? 10 : mHotCarBrandList.size();
    }

    public static class HotCarBrandItemHolder extends BaseHolder {
        ImageView ivBrand;
        TextView tvBrandName;

        public HotCarBrandItemHolder(View itemView) {
            super(itemView);
            ivBrand = findViewById(R.id.iv_brand);
            tvBrandName = findViewById(R.id.tv_brand_name);
        }
    }

    public void setData(ArrayList<CarBrandBean> hotCarBrandList) {
        if (hotCarBrandList == null) return;
        mHotCarBrandList.clear();
        mHotCarBrandList.addAll(hotCarBrandList);
        notifyDataSetChanged();
    }

    private OnHotItemClickListner onHotItemClickListner;

    public void setOnHotItemClickListner(OnHotItemClickListner onHotItemClickListner) {
        this.onHotItemClickListner = onHotItemClickListner;
    }

    public interface OnHotItemClickListner{
        void onHotItemClick(CarBrandBean carBrandBean,int position);
    }
}
